// packageFT/hotel/grabbing-order/grabbing-order.js
const app = getApp()
import { roomTypeEnum, demandPayStatusEnum } from '../../../utils/dataEnum'
import { dateRange, timeDiff, formatTime } from '../../../utils/util'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    roomTypeEnum,
    demandPayStatusEnum,
    timeout: 0,
    // 请求参数对象
    queryObj:{
      page: 0,
      page_size: 5
    },
    // 总数量，用来实现分页
    total: 0,
    // 是否正在请求数据
    isloading: false,
    finished: false
  },

  getOrderList(callback) {
    const urlStr = app.getNetAddresss('plugin.hotel.frontend.hotel.hotelDemand.order-grabbing-hall-list')
    const _this = this
    wx.showLoading({
      title: '加载中...',
    })
    app._getNetWork({
      url: urlStr,
      data: {
        limit: this.data.queryObj.page_size,
        offset: this.data.list.length
      },
      success: function(res) {
        const resData = res.data
        if (resData.result == 1) {
          let resList = [...resData.data.list]
          const nowStr = formatTime(new Date())
          resList.forEach(item => {
            const diffMillisecond = timeDiff(nowStr, item.valid_time, 'millisecond')
            item.timeRange = dateRange(item.start_check_in_time, item.end_check_in_time, true),
            item.countDown = diffMillisecond
          })
          _this.setData({
            total: resData.data.count,
            timeout: resData.data.cron_close_time || 0,
            list: [..._this.data.list, ...resList]
          })
          _this.setData({
            finished: _this.data.list.length >= _this.data.total
          })
        } else {
          app.tips(resData.msg || '数据获取失败！')
        }
      },
      complete: function() {
        wx.hideLoading()
      }
    })
  },

  /**
   * 跳转到抢单详情
   * @param {Object}} e 
   */
  goOrderDetail: function(e) {
    const { id, valid_time } = e.currentTarget.dataset.item
    const nowStr = formatTime(new Date())
    const diffMillisecond = timeDiff(nowStr, valid_time, 'millisecond')
    if (diffMillisecond == 0) {
      app.tips('订单超时！')
      return
    }
    wx.navigateTo({
      url: '/packageFT/hotel/grabbing-order-detail/grabbing-order-detail?oid=' + id,
    })
  },

  /**
   * 页面刷新
   * @param {Function} callback 回调函数
   */
  refreshPage(callback) {
    this.setData({
      list: [],
      queryObj: {
        page: 0,
        page_size: 5
      },
      total: 0,
      isloading: false,
      finished: false
    })
    this.getOrderList(callback)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getOrderList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.refreshPage(() => wx.stopPullDownRefresh())
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if ((this.data.queryObj.page + 1) * this.data.queryObj.page_size >= this.data.total){
      wx.showLoading({
        title: '数据加载完毕！',
      })
      wx.hideLoading() // 关闭loading
      return
    }
    if(this.data.isLoading) return
    this.setData({
      // 让页码值自增 +1
      page: this.data.queryObj.page += 1
    })
    // 重新获取列表数据
    this.getOrderList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})